package lab.gene.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import lab.validate.lib.ValidateLibrary;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lab.gene.service.impl.GeneImageFileServiceImpl;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

public class UploadImageAction extends ActionSupport implements ServletResponseAware, ServletRequestAware{
	
	private static final long serialVersionUID = 1L;
	private File imageFile;
	private String imageFileFileName;
	private String userName;
	private String formName;
	private String geneName;
	private String path;
	
	protected HttpServletResponse servletResponse;  
	@Override   
	public void setServletResponse(HttpServletResponse servletResponse) {   
		this.servletResponse = servletResponse;   
	}   
	
	protected HttpServletRequest servletRequest;   
	@Override   
	public void setServletRequest(HttpServletRequest servletRequest) {   
		this.servletRequest = servletRequest;   
	}
	
	public String execute() {
		String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
		filePath = filePath +"upload"+"\\";
        System.out.println("Server path:" + filePath);

	      try{
	    	  System.out.println("uploading!!!!");
	     	  System.out.println("Src File name: " + imageFile);
	     	  System.out.println("Dst File name: " + imageFileFileName);
   	     	  GeneImageFileServiceImpl service = new GeneImageFileServiceImpl();
	     	 
	     	  Cookie[] cookies = servletRequest.getCookies();
	    	  String cookieName = "LabUser";
	    	  userName = null;
	    	  if (cookies != null) {
	    		  for (int i = 0; i < cookies.length; i++) {
	    			  if (cookies[i].getName().equals(cookieName)) {
	    				  userName = cookies[i].getValue();
	    				  break;
	    			  }
	    		  }
	    	  }
	    		
	    	 String timeStamp = String.valueOf(System.currentTimeMillis());
	    	 String[] fName = imageFileFileName.split("\\.");
	    	 imageFileFileName = fName[0] + "_" + timeStamp + "." + fName[1];
	    	 System.out.println("uploading image......");
	         System.out.println(imageFileFileName);
	    		 
	         File destFile  = new File(filePath, imageFileFileName);
		     FileUtils.copyFile(imageFile, destFile);
		     service.addGeneImageFile(geneName, userName, formName, imageFileFileName);
		     
	      }catch(IOException e){
	         e.printStackTrace();
	         return "error";
	      }
	      
		return "success";
	}
	
	public void validate() {

		if (imageFile ==  null || imageFileFileName == null || imageFile.length() > 5242880) {
			addFieldError("error", "Please choose a valid file (size < 5M)");
		}
		try {
			FileInputStream is = new FileInputStream (imageFile);
			String type = ValidateLibrary.getTypeByStream(is);
			if (type != "jpg"&& type != "png"&&type!="gif"&&type!="tif"&&type!="bmp") {
				addFieldError("error","Please choose a valid file (jpg png gif tif bmp)");
			}
		}	
		catch (Exception e){
			e.printStackTrace();
			addFieldError("userImage", "Invalid File!");
		}
	}
	

	public File getImageFile() {
		return imageFile;
	}

	public void setImageFile(File imageFile) {
		this.imageFile = imageFile;
	}

	public String getImageFileFileName() {
		return imageFileFileName;
	}

	public void setImageFileFileName(String imageFileFileName) {
		this.imageFileFileName = imageFileFileName;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getGeneName() {
		return geneName;
	}

	public void setGeneName(String geneName) {
		this.geneName = geneName;
	}
	
	

}
